﻿#region 文件信息

// FileName: SoCafeShop/Sohobloo.SoAuth.Service/ActionService.cs
// Author: Sohobloo
// CreateTime: 2013/06/30 11:51
// UpdateTime: 2013/06/30 12:08

#endregion

#region 引用

using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Linq;
using Sohobloo.SoAuth.Data.Repositories;
using Sohobloo.SoAuth.Entity;
using Sohobloo.SoMcCoffee.Public.OperationResult;

#endregion

namespace Sohobloo.SoAuth.Service.Impl
{
    [Export(typeof (IRoleService))]
    internal class RoleService : ServiceBase, IRoleService
    {
        #region 属性

        [Import]
        IRoleRepository RoleRepository { get; set; }

        [Import]
        IPermissionRepository PermissionRepository { get; set; }

        [Import]
        IUserRepository UserRepository { get; set; }

        #endregion 属性

        #region Implementation of IRoleService

        public OperationResult CreateRole(SoAuthRole role)
        {
            //检查角色名称重复
            if (RoleRepository.Entities.Any(r => r.RoleName == role.RoleName))
            {
                return new OperationResult(OperationResultType.Conflict, "角色名称重复");
            }

            RoleRepository.Insert(role, false);

            return UnitOfWork.Commit() > 0
                       ? new OperationResult(OperationResultType.Success)
                       : new OperationResult(OperationResultType.NoChange);
        }

        public OperationResult DeleteRole(Guid roleId)
        {
            var role = RoleRepository.GetByKey(roleId);
            PermissionRepository.Delete(role.RolePermissions, false);

            RoleRepository.Delete(role, false);

            return UnitOfWork.Commit() > 0
                       ? new OperationResult(OperationResultType.Success)
                       : new OperationResult(OperationResultType.NoChange);
        }

        public OperationResult UpdateRole(SoAuthRole role)
        {
            if (RoleRepository.Entities.Any(r => r.Id != role.Id && r.RoleName == role.RoleName))
            {
                return new OperationResult(OperationResultType.Conflict, "角色名称重复");
            }

            RoleRepository.Update(role, false);

            return UnitOfWork.Commit() > 0
                       ? new OperationResult(OperationResultType.Success)
                       : new OperationResult(OperationResultType.Conflict);
        }

        public ICollection<SoAuthRole> GetRoleList()
        {
            return RoleRepository.Entities.OrderBy(r => r.CreateTime).ToList();
        }

        public SoAuthRole GetRole(Guid roleId)
        {
            return RoleRepository.GetByKey(roleId);
        }

        public OperationResult AddUser(Guid roleId, string userName)
        {
            var role = GetRole(roleId);
            if(role.RoleUsers.Any(u => u.UserName == userName))
            {
                return new OperationResult(OperationResultType.Conflict, "角色用户已存在");
            }

            var user = UserRepository.Entities.SingleOrDefault(u => u.UserName == userName);
            if(user == null)
            {
                return new OperationResult(OperationResultType.Conflict, "用户名不存在");
            }

            role.RoleUsers.Add(user);

            return UnitOfWork.Commit() > 0
                ? new OperationResult(OperationResultType.Success)
                : new OperationResult(OperationResultType.Conflict);
        }

        public OperationResult RemoveUser(Guid roleId, Guid userId)
        {
            var role = GetRole(roleId);
            var user = UserRepository.GetByKey(userId);
            if(user == null)
            {
                return new OperationResult(OperationResultType.Conflict, "用户不存在");
            }

            role.RoleUsers.Remove(user);

            return UnitOfWork.Commit() > 0
                ? new OperationResult(OperationResultType.Success)
                : new OperationResult(OperationResultType.Conflict);
        }

        #endregion
    }
}